import request from '@/utils/request'

/**
 * 消息通知API接口
 * 包含系统消息管理、消息推送、消息中心管理、消息统计等功能
 */

// ================== 系统消息管理 ==================

// 发送系统消息给指定用户
export function sendSystemMessage(userId, messageData) {
  return request({
    url: `/api/messages/system/send/${userId}`,
    method: 'post',
    data: messageData
  })
}

// 广播系统消息给所有用户
export function broadcastSystemMessage(messageData) {
  return request({
    url: '/api/messages/system/broadcast',
    method: 'post',
    data: messageData
  })
}

// 发送系统消息给指定角色用户
export function sendSystemMessageToRole(roleId, messageData) {
  return request({
    url: `/api/messages/system/send-role/${roleId}`,
    method: 'post',
    data: messageData
  })
}

// 获取系统消息列表
export function getSystemMessageList(currentPage, pageSize, queryData) {
  return request({
    url: `/api/messages/system/list/${currentPage}/${pageSize}`,
    method: 'get',
    params: queryData
  })
}

// 获取系统消息详情
export function getSystemMessageById(messageId) {
  return request({
    url: `/api/messages/system/${messageId}`,
    method: 'get'
  })
}

// 更新系统消息
export function updateSystemMessage(messageData) {
  return request({
    url: '/api/messages/system',
    method: 'put',
    data: messageData
  })
}

// 删除系统消息
export function deleteSystemMessage(messageId) {
  return request({
    url: `/api/messages/system/${messageId}`,
    method: 'delete'
  })
}

// ================== 消息中心管理 ==================

// 获取用户消息列表
export function getUserMessageList(currentPage, pageSize, queryData) {
  return request({
    url: `/api/messages/center/list/${currentPage}/${pageSize}`,
    method: 'get',
    params: queryData
  })
}

// 获取用户消息详情
export function getUserMessageById(messageId) {
  return request({
    url: `/api/messages/center/${messageId}`,
    method: 'get'
  })
}

// 标记消息为已读
export function markMessageAsRead(messageId) {
  return request({
    url: `/api/messages/center/mark-read/${messageId}`,
    method: 'put'
  })
}

// 批量标记消息为已读
export function batchMarkMessagesAsRead(messageIds) {
  return request({
    url: '/api/messages/center/batch-mark-read',
    method: 'put',
    data: messageIds
  })
}

// 标记消息为未读
export function markMessageAsUnread(messageId) {
  return request({
    url: `/api/messages/center/mark-unread/${messageId}`,
    method: 'put'
  })
}

// 删除用户消息
export function deleteUserMessage(messageId) {
  return request({
    url: `/api/messages/center/${messageId}`,
    method: 'delete'
  })
}

// 批量删除用户消息
export function batchDeleteUserMessages(messageIds) {
  return request({
    url: '/api/messages/center/batch-delete',
    method: 'delete',
    data: messageIds
  })
}

// 清空用户所有消息
export function clearAllUserMessages() {
  return request({
    url: '/api/messages/center/clear-all',
    method: 'delete'
  })
}

// ================== 消息推送功能 ==================

// 推送单条消息
export function pushSingleMessage(pushData) {
  return request({
    url: '/api/messages/push/single',
    method: 'post',
    data: pushData
  })
}

// 批量推送消息
export function batchPushMessages(pushData) {
  return request({
    url: '/api/messages/push/batch',
    method: 'post',
    data: pushData
  })
}

// 推送给指定用户列表
export function pushMessagesToUsers(userIds, pushData) {
  return request({
    url: '/api/messages/push/to-users',
    method: 'post',
    data: {
      userIds,
      ...pushData
    }
  })
}

// 推送给指定角色
export function pushMessagesToRole(roleId, pushData) {
  return request({
    url: `/api/messages/push/to-role/${roleId}`,
    method: 'post',
    data: pushData
  })
}

// 设置推送配置
export function setPushConfig(configData) {
  return request({
    url: '/api/messages/push/config',
    method: 'post',
    data: configData
  })
}

// 获取推送配置
export function getPushConfig() {
  return request({
    url: '/api/messages/push/config',
    method: 'get'
  })
}

// 测试推送功能
export function testPushMessage(testData) {
  return request({
    url: '/api/messages/push/test',
    method: 'post',
    data: testData
  })
}

// ================== 消息统计功能 ==================

// 获取用户消息统计
export function getUserMessageStatistics() {
  return request({
    url: '/api/messages/statistics/user',
    method: 'get'
  })
}

// 获取管理员消息统计
export function getAdminMessageStatistics() {
  return request({
    url: '/api/messages/statistics/admin',
    method: 'get'
  })
}

// 获取消息发送统计
export function getMessageSendStatistics(startDate, endDate) {
  return request({
    url: '/api/messages/statistics/send',
    method: 'get',
    params: { startDate, endDate }
  })
}

// 获取消息类型统计
export function getMessageTypeStatistics() {
  return request({
    url: '/api/messages/statistics/type',
    method: 'get'
  })
}

// 获取用户活跃度统计
export function getUserActivityStatistics() {
  return request({
    url: '/api/messages/statistics/activity',
    method: 'get'
  })
}

// 获取未读消息数量
export function getUnreadMessageCount() {
  return request({
    url: '/api/messages/statistics/unread-count',
    method: 'get'
  })
}

// ================== 消息撤回功能 ==================

// 撤回单条消息
export function recallMessage(messageId, reason = '') {
  return request({
    url: `/api/messages/recall/${messageId}`,
    method: 'put',
    params: { reason }
  })
}

// 批量撤回消息
export function batchRecallMessages(messageIds, reason = '') {
  return request({
    url: '/api/messages/recall/batch',
    method: 'put',
    data: {
      messageIds,
      reason
    }
  })
}

// 获取可撤回的消息列表
export function getRecallableMessageList(currentPage, pageSize) {
  return request({
    url: `/api/messages/recall/list/${currentPage}/${pageSize}`,
    method: 'get'
  })
}

// 检查消息是否可撤回
export function checkMessageRecallable(messageId) {
  return request({
    url: `/api/messages/recall/check/${messageId}`,
    method: 'get'
  })
}

// ================== 批量操作功能 ==================

// 批量发送消息
export function batchSendMessages(batchData) {
  return request({
    url: '/api/messages/batch/send',
    method: 'post',
    data: batchData
  })
}

// 批量标记消息
export function batchMarkMessages(markData) {
  return request({
    url: '/api/messages/batch/mark',
    method: 'put',
    data: markData
  })
}

// 批量删除消息
export function batchDeleteMessages(deleteData) {
  return request({
    url: '/api/messages/batch/delete',
    method: 'delete',
    data: deleteData
  })
}

// 批量导出消息
export function batchExportMessages(exportData) {
  return request({
    url: '/api/messages/batch/export',
    method: 'post',
    data: exportData,
    responseType: 'blob'
  })
}

// 批量导入消息
export function batchImportMessages(file) {
  const formData = new FormData()
  formData.append('file', file)

  return request({
    url: '/api/messages/batch/import',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// ================== 消息模板管理 ==================

// 获取消息模板列表
export function getMessageTemplateList(currentPage, pageSize, queryData) {
  return request({
    url: `/api/messages/template/list/${currentPage}/${pageSize}`,
    method: 'get',
    params: queryData
  })
}

// 创建消息模板
export function createMessageTemplate(templateData) {
  return request({
    url: '/api/messages/template',
    method: 'post',
    data: templateData
  })
}

// 更新消息模板
export function updateMessageTemplate(templateData) {
  return request({
    url: '/api/messages/template',
    method: 'put',
    data: templateData
  })
}

// 删除消息模板
export function deleteMessageTemplate(templateId) {
  return request({
    url: `/api/messages/template/${templateId}`,
    method: 'delete'
  })
}

// 使用消息模板发送消息
export function sendMessageWithTemplate(templateId, templateData) {
  return request({
    url: `/api/messages/template/send/${templateId}`,
    method: 'post',
    data: templateData
  })
}

// ================== 消息设置管理 ==================

// 获取消息设置
export function getMessageSettings() {
  return request({
    url: '/api/messages/settings',
    method: 'get'
  })
}

// 更新消息设置
export function updateMessageSettings(settingsData) {
  return request({
    url: '/api/messages/settings',
    method: 'put',
    data: settingsData
  })
}

// 获取用户消息偏好设置
export function getUserMessagePreferences() {
  return request({
    url: '/api/messages/settings/preferences',
    method: 'get'
  })
}

// 更新用户消息偏好设置
export function updateUserMessagePreferences(preferencesData) {
  return request({
    url: '/api/messages/settings/preferences',
    method: 'put',
    data: preferencesData
  })
}

// ================== 消息搜索和过滤 ==================

// 搜索消息
export function searchMessages(searchData) {
  return request({
    url: '/api/messages/search',
    method: 'get',
    params: searchData
  })
}

// 高级搜索消息
export function advancedSearchMessages(searchData) {
  return request({
    url: '/api/messages/search/advanced',
    method: 'post',
    data: searchData
  })
}

// 按日期范围筛选消息
export function filterMessagesByDate(startDate, endDate, filterData) {
  return request({
    url: '/api/messages/filter/date',
    method: 'get',
    params: {
      startDate,
      endDate,
      ...filterData
    }
  })
}

// 按类型筛选消息
export function filterMessagesByType(messageType, filterData) {
  return request({
    url: `/api/messages/filter/type/${messageType}`,
    method: 'get',
    params: filterData
  })
}

// 按状态筛选消息
export function filterMessagesByStatus(status, filterData) {
  return request({
    url: `/api/messages/filter/status/${status}`,
    method: 'get',
    params: filterData
  })
}